import { createRouter, createWebHistory } from 'vue-router'
import Home from '../views/home/Home.vue'
import ModernHome from '../views/home/ModernHome.vue'
import Layout from '../components/layout/Layout.vue'
import { useUserStore } from '../stores/user'

const routes = [
    {
        path: '/',
        component: Layout,
        children: [
            {
                path: '',
                name: 'Home',
                component: Home,
                meta: {
                    title: '首页',
                    keepAlive: true
                }
            },
            {
                path: 'modern',
                name: 'ModernHome',
                component: ModernHome,
                meta: {
                    title: '现代首页',
                    keepAlive: true
                }
            },
            {
                path: 'map',
                name: 'Map',
                component: () => import('../views/map/Map.vue'),
                meta: {
                    title: '地图',
                    keepAlive: true
                }
            },
            {
                path: '/login',
                name: 'Login',
                component: () => import('../views/Login.vue'),
                meta: {
                    title: '登录',
                    keepAlive: false
                }
            },
            {
                path: '/register',
                name: 'Register',
                component: () => import('../views/Register.vue'),
                meta: {
                    title: '注册',
                    keepAlive: false
                }
            },
            {
                path: '/product/:id',
                name: 'ProductDetail',
                component: () => import('../views/product/ProductDetail.vue'),
                meta: {
                    title: '商品详情',
                    keepAlive: false
                }
            },
            {
                path: '/category',
                name: 'Category',
                component: () => import('../views/category/Category.vue'),
                meta: {
                    title: '分类',
                    keepAlive: true
                }
            },
            {
                path: '/category/:id',
                name: 'CategoryDetail',
                component: () => import('../views/category/CategoryDetail.vue'),
                meta: {
                    title: '分类详情',
                    keepAlive: false
                }
            },
            {
                path: '/search',
                name: 'Search',
                component: () => import('../views/search/Search.vue'),
                meta: {
                    title: '搜索',
                    keepAlive: false
                }
            },
            {
                path: '/school/:id',
                name: 'SchoolDetail',
                component: () => import('../views/school/SchoolDetail.vue'),
                meta: {
                    title: '学校详情',
                    keepAlive: false
                }
            },
            {
                path: '/schools',
                name: 'Schools',
                component: () => import('../views/school/Schools.vue'),
                meta: {
                    title: '学校列表',
                    keepAlive: true
                }
            },
            {
                path: '/user/profile',
                name: 'Profile',
                component: () => import('../views/user/Profile.vue'),
                meta: {
                    title: '个人资料',
                    keepAlive: false
                }
            },
            {
                path: '/user/center',
                name: 'UserCenter',
                component: () => import('../views/user/UserCenter.vue'),
                meta: {
                    title: '个人中心',
                    keepAlive: true
                }
            },
            {
                path: '/user/orders',
                name: 'Orders',
                component: () => import('../views/user/Orders.vue'),
                meta: {
                    title: '我的订单',
                    keepAlive: true
                }
            },
            {
                path: '/user/favorites',
                name: 'Favorites',
                component: () => import('../views/user/Favorites.vue'),
                meta: {
                    title: '我的收藏',
                    keepAlive: true
                }
            },
            {
                path: '/user/messages',
                name: 'Messages',
                component: () => import('../views/user/UserCenter.vue'),
                meta: {
                    title: '消息中心',
                    keepAlive: true
                }
            },
            {
                path: '/user/wallet',
                name: 'Wallet',
                component: () => import('../views/user/UserCenter.vue'),
                meta: {
                    title: '我的钱包',
                    keepAlive: true
                }
            },
            {
                path: '/user/settings',
                name: 'Settings',
                component: () => import('../views/user/UserCenter.vue'),
                meta: {
                    title: '设置',
                    keepAlive: true
                }
            },
            {
                path: '/cart',
                name: 'Cart',
                component: () => import('@/views/cart/Cart.vue'),
                meta: {
                    title: '购物车',
                    requiresAuth: true
                }
            },
            {
                path: 'chat',
                name: 'Chat',
                component: () => import('../views/chat/Chat.vue'),
                meta: {
                    title: '聊天',
                    keepAlive: true
                }
            },
            {
                path: 'requests',
                name: 'Requests',
                component: () => import('../views/requests/Requests.vue'),
                meta: {
                    title: '用户求购',
                    keepAlive: true
                }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const userStore = useUserStore()
    const publicPages = ['/login', '/register', '/', '/school', '/product', '/map', '/category']
    const authRequired = !publicPages.some(path => to.path.startsWith(path))

    if (to.meta.requiresAuth && !userStore.isLoggedIn) {
        next({
            path: '/login',
            query: { redirect: to.fullPath }
        })
    } else if (authRequired && !userStore.isLoggedIn) {
        next('/login')
    } else if ((to.path === '/login' || to.path === '/register') && userStore.isLoggedIn) {
        next('/')
    } else {
        next()
    }
})

export default router 